<?php
/**
 * Pert PHP Library
 * Page Class
 *
 * @author	Emre Hasegeli <hasegeli@gmail.com>
 * @license	GNU General Public License v3
 */

namespace Pert;

class Page
{
	private $hREF;

	private $content;

	private static $object;

	private function __Construct()
	{
		$this->hREF = new Page_HREF;

		$this->content = new Page_Content;

		self::$object = $this;
	}

	public static function Get()
	{
		if(empty(self::$object))
			new self();

		return self::$object;
	}

	public function HREF()
	{
		return $this->hREF;
	}

	public function Content()
	{
		return $this->content;
	}

	private $titleArray;

	public function AddTitle($title)
	{
		$this->titleArray[] = $title;

		return true;
	}

	public function TitleArray()
	{
		if(isset($this->titleArray))
			return $this->titleArray;

		return array();
	}

	public function TitleString()
	{
		$string = '';

		foreach($this->TitleArray() AS $title)
		{
			if($string)
				$string .= ' - ';

			$string .= $title;
		}

		return $string;
	}

	private $scriptArray;

	public function AddScript($script)
	{
		$this->scriptArray[] = $script;

		return true;
	}

	public function HeaderHTML()
	{
		$hTML = '<?xml version="1.0" encoding="utf-8" ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" >

<html xmlns="http://www.w3.org/1999/xhtml" >
	<head >
		<meta http-equiv="Content-Type"
				content="text/html;charset=UTF-8" />

		<title >' . $this->TitleString() . '
		</title>';

		if(isset($this->scriptArray))
			foreach($this->scriptArray AS $script)
				$hTML .= '

		<script type="text/javascript" >' . $script . '
		</script>';

		$hTML .= '
	</head>

	<body >';

		return $hTML;
	}

	public function FooterHTML()
	{
		return '
	</body>
</html>
';
	}

	public function RefreshHTML()
	{
		return '<?xml version="1.0" encoding="utf-8" ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" >

<html xmlns="http://www.w3.org/1999/xhtml" >
	<head >
		<meta http-equiv="Refresh"
				content="0">

		<title >' . $this->TitleString() . '
		</title>
	</head>
</html>
';
	}

	public function RedirectHTML($URL)
	{
		return '<?xml version="1.0" encoding="utf-8" ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" >

<html xmlns="http://www.w3.org/1999/xhtml" >
	<head >
		<meta http-equiv="Refresh"
				content="0;url=' . $URL . '">

		<title >' . $this->TitleString() . '
		</title>
	</head>
</html>
';
	}
}

class Page_HREF
{
	private $parameterArray;

	public function __ToString()
	{
		$hREF = '';

		if(isset($this->parameterArray))
		{
			$hREF .= '?';

			$count = 0;

			foreach($this->parameterArray AS $parameterId => $parameter)
			{
				if($count++)
					$hREF .= '&';

				$hREF .= $parameterId . '=' . $parameter;
			}
		}

		return $hREF;
	}

	public function __Isset($parameterId)
	{
		if(empty($this->parameterArray[$parameterId]))
			if(isset($_GET[$parameterId]))
			{
				require_once __DIR__ . '/Link.php';

				if(Link::ParameterControl($_GET[$parameterId]))
					$this->parameterArray[$parameterId] =& $_GET[$parameterId];
			}

		if(empty($this->parameterArray[$parameterId]))
			return false;

		return true;
	}

	public function __Get($parameterId)
	{
		if($this->__Isset($parameterId))
			return $this->parameterArray[$parameterId];
	}

	public function __Set($parameterId,
			$parameter)
	{
		require_once __DIR__ . '/Link.php';

		if(!Link::ParameterControl($parameter))
			return false;

		$this->parameterArray[$parameterId] = $parameter;

		return true;
	}

	public function __Unset($parameterId)
	{
		if(!$this->__Isset($parameterId))
			return false;

		unset($this->parameterArray[$parameterId]);

		return true;
	}
}

class Page_Content
{
	private $parameterArray;

	public function __Isset($parameterId)
	{
		if(!isset($this->parameterArray[$parameterId]))
			return false;

		return true;
	}

	public function __Get($parameterId)
	{
		if($this->__Isset($parameterId))
			return $this->parameterArray[$parameterId];
	}

	public function __Set($parameterId,
			$parameter)
	{
		if(isset($this->parameterArray[$parameterId]))
			return false;

		$this->parameterArray[$parameterId] = $parameter;

		return true;
	}

	public function __Unset($parameterId)
	{
		if(!$this->__Isset($parameterId))
			return false;

		unset($this->parameterArray[$parameterId]);

		return true;
	}
}
